/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vetAdmin.dao.implementation;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import vetAdmin.dao.ClienteDAO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;

import javax.swing.JOptionPane;

import vetAdmin.model.Cliente;
import vetAdmin.model.Paciente;

/**
 *
 * @author Gustavo
 */
public class CombosClienteDAOJDBC {


    private static Object[][] ResultSetToArray(ResultSet rs)
    {
        Object obj[][]=null;
        try
        {
            rs.last();
            ResultSetMetaData rsmd = rs.getMetaData();
            int numCols = rsmd.getColumnCount();
            int numFils =rs.getRow();
            obj=new Object[numFils][numCols];
            int j = 0;
            rs.beforeFirst();
            while (rs.next())
            {
                for (int i=0;i<numCols;i++)
                {
                    obj[j][i]=rs.getObject(i+1);
                }
                j++;
            }
        }
        catch(Exception e)
        {
        }
        return obj;
    }    
    
    
    
    
    public static String[] getLocalidades(Connection conn) throws SQLException {
        String laConsulta = "SELECT * FROM veterinaria.localidad;";

        Statement stmtConsulta = conn.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[] auxiliar;
        auxiliar= new String[vector.length+1];
        
        auxiliar[0]="Seleccione";        
        for (int i=0;i< vector.length;i++){
            auxiliar[i+1]=vector[i][2].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;
    }
    

    public static String[][] getLocalidades(Connection conn, String prov) throws SQLException {
        String laConsulta = "SELECT * FROM veterinaria.localidad WHERE id_provincia="
                          + "(SELECT id_provincia FROM veterinaria.provincia WHERE provincia like '"+prov+"');";

        Statement stmtConsulta = conn.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

/*        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[] auxiliar;
        auxiliar= new String[vector.length+1];
        
        auxiliar[0]="Seleccione";        
        for (int i=0;i< vector.length;i++){
            auxiliar[i+1]=vector[i][2].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;*/
        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[][] auxiliar;
        String[] a=new String[2];
        //auxiliar= new String[vector.length+1];
        auxiliar= new String[vector.length+1][2];

        auxiliar[0][0]="0";        
        auxiliar[0][1]="Seleccione";

        for (int i=0;i< vector.length;i++){
            auxiliar[i+1][0]=vector[i][0].toString();
            auxiliar[i+1][1]=vector[i][2].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;
        
    }    
    

    public static String[] getProvincias(Connection conn) throws SQLException {
        String laConsulta = "SELECT * FROM veterinaria.provincia;";

        Statement stmtConsulta = conn.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[] auxiliar;
        auxiliar= new String[vector.length+1];
        
        auxiliar[0]="Seleccione";        
        for (int i=0;i< vector.length;i++){
            auxiliar[i+1]=vector[i][1].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;
    }    
    
    public static String[] getEspecies(Connection conn) throws SQLException {
        String laConsulta = "SELECT * FROM veterinaria.especie;";

        Statement stmtConsulta = conn.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[] auxiliar;
        auxiliar= new String[vector.length+1];
        
        auxiliar[0]="Seleccione";        
        for (int i=0;i< vector.length;i++){
            auxiliar[i+1]=vector[i][1].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;
    }       
    
    
    public static String[] getRazas(Connection conn) throws SQLException {
        String laConsulta = "SELECT * FROM veterinaria.raza;";

        Statement stmtConsulta = conn.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[] auxiliar;
        auxiliar= new String[vector.length+1];
        
        auxiliar[0]="Seleccione";
        for (int i=0;i< vector.length;i++){
            auxiliar[i+1]=vector[i][2].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;
    }           

    
    
    public static String[][] getRazas(Connection conn,String especie) throws SQLException {
        String laConsulta = "SELECT * FROM veterinaria.raza WHERE id_especie="
                          + "(SELECT id_especie FROM veterinaria.especie WHERE especie like '"+especie+"');";

        Statement stmtConsulta = conn.createStatement();
        ResultSet rs = stmtConsulta.executeQuery(laConsulta);

        Object[][] vector=ResultSetToArray(rs);
        
        //DefaultListModel aux=new DefaultListModel();
        String[][] auxiliar;
        String[] a=new String[2];
        //auxiliar= new String[vector.length+1];
        auxiliar= new String[vector.length+1][2];

        auxiliar[0][0]="0";        
        auxiliar[0][1]="Seleccione";

        for (int i=0;i< vector.length;i++){
            auxiliar[i+1][0]=vector[i][0].toString();
            auxiliar[i+1][1]=vector[i][2].toString().toUpperCase();
        }
        
        
        // Cierra el Statement y la Connection
        stmtConsulta.close();
        conn.close();
        return auxiliar;
    }           
    
    
}
